
package com.sun.showcase.biz.mall.service.impl;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sun.showcase.biz.mall.dao.AddressDao;
import com.sun.showcase.biz.mall.service.AddressService;
import com.sun.showcase.client.domain.mall.Address;
import com.sun.showcase.client.query.mall.AddressQuery;
import com.sun.showcase.pojo.DataGrid;
import com.sun.showcase.pojo.Pager;
import com.sun.showcase.utils.StringConstant;
import com.sun.showcase.utils.UuidUtil;
@Service("addressService")
@Transactional
public class AddressServiceImpl implements AddressService{
	@Autowired
	private AddressDao addressDao;
	
	public void setAddressDao(AddressDao dao) {
		this.addressDao = dao;
	}

	public DataGrid datagrid(AddressQuery addressQuery) {
		DataGrid j = new DataGrid();
		Pager<Address> pager  = find(addressQuery);
		j.setRows(getQuerysFromEntitys(pager.getRecords()));
		j.setTotal(pager.getTotalRecords());
		return j;
	}

	private List<AddressQuery> getQuerysFromEntitys(List<Address> addresss) {
		List<AddressQuery> addressQuerys = new ArrayList<AddressQuery>();
		if (addresss != null && addresss.size() > 0) {
			for (Address tb : addresss) {
				AddressQuery b = new AddressQuery();
				BeanUtils.copyProperties(tb, b);
				addressQuerys.add(b);
			}
		}
		return addressQuerys;
	}

	private Pager<Address> find(AddressQuery addressQuery) {
		Page<Address> page = PageHelper.startPage(addressQuery.getPage().intValue(), addressQuery.getRows().intValue());
		List<Address> list = addressDao.findList(addressQuery);
		Pager<Address> pager = new Pager<Address>();
		pager.setRecords(list);
		pager.setTotalRecords(page.getTotal());
		return  pager;
		
	}
	


	public Address add(AddressQuery addressQuery) {
		Address t = new Address();
		BeanUtils.copyProperties(addressQuery, t);
		t.setActiveFlag(StringConstant.ONE);
		t.setCreateDate(new Date());
	    t.setId(UuidUtil.get32UUID());
		addressDao.save(t);
		addressQuery.setId(t.getId());
		return t;
		
	}

	public void update(AddressQuery addressQuery) {
		Address t = addressDao.getById(addressQuery.getId());
	    if(t != null) {
	    	BeanUtils.copyProperties(addressQuery, t);
	    	addressDao.update(t);
		}
	}
	
	/**
	 * 根据条件更新部分字段   新建一个query封装需要更新的字段
	 * searchMap封装更新条件
	 * */
	public void updatePart(AddressQuery addressQuery) {
	    addressDao.updatePart(addressQuery);
	}

	public void delete(java.lang.String[] ids) {
		addressDao.deleteAc(ids);
	}
	
	//逻辑删除 更新状态位
	public void deletePt(java.lang.String[] ids) {
		addressDao.deletePt(ids);
	}

	public Address get(AddressQuery addressQuery) {
		return addressDao.getById(addressQuery.getId());
	}

	public Address get(String id) {
		return addressDao.getById(id);
	}

	
	public List<AddressQuery> listAll(AddressQuery addressQuery) {
	    List<Address> list = addressDao.findList(addressQuery);
		List<AddressQuery> listQuery =getQuerysFromEntitys(list) ;
		return listQuery;
	}
	
	
}
